<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>性能排查常用命令 - macdfree&#39;s blog</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="macd" /><meta name="description" content="性能排查常用命令" />
<meta name="keywords" content="性能排查" />



<meta name="baidu-site-verification" content="Ak74Sk61bk" />
<meta name="google-site-verification" content="fAeFHpkrhfFS8LxdzXy0EtAPcVStv0faGn238sRF3L8" />


<meta name="generator" content="Hugo 0.54.0" />


<link rel="canonical" href="/wiki/java/%E6%80%A7%E8%83%BD%E6%8E%92%E6%9F%A5%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" />

<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">




<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>


<link href="/dist/even.min.css?v=3.1.1" rel="stylesheet">
<link href="/lib/fancybox/jquery.fancybox-3.1.20.min.css" rel="stylesheet">




<meta property="og:title" content="性能排查常用命令" />
<meta property="og:description" content="性能排查常用命令" />
<meta property="og:type" content="article" />
<meta property="og:url" content="/wiki/java/%E6%80%A7%E8%83%BD%E6%8E%92%E6%9F%A5%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" />
<meta property="article:published_time" content="2018-09-15T21:44:42&#43;08:00"/>
<meta property="article:modified_time" content="2018-09-15T21:44:42&#43;08:00"/>

<meta itemprop="name" content="性能排查常用命令">
<meta itemprop="description" content="性能排查常用命令">


<meta itemprop="datePublished" content="2018-09-15T21:44:42&#43;08:00" />
<meta itemprop="dateModified" content="2018-09-15T21:44:42&#43;08:00" />
<meta itemprop="wordCount" content="569">



<meta itemprop="keywords" content="性能排查," />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="性能排查常用命令"/>
<meta name="twitter:description" content="性能排查常用命令"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">macdfree&#39;s blog</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/archive/">
        <li class="mobile-menu-item">归档</li>
      </a>
  </ul>
</nav>
  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">macdfree&#39;s blog</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/archive/">归档</a>
      </li>
  </ul>
</nav>
    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">性能排查常用命令</h1>

      <div class="post-meta">
        <span class="post-time"> 2018-09-15 </span>
        <div class="post-category">
          
            <a href="/categories/java/"> java </a>
          
        </div>
        <span class="more-meta"> 约 569 字 </span>
        <span class="more-meta"> 预计阅读 2 分钟 </span>
        <span id="busuanzi_container_page_pv" class="more-meta"> <span id="busuanzi_value_page_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次阅读 </span>
      </div>
    </header>

    
    
<div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
<ul>
<li><a href="#数据库层面">数据库层面</a></li>
<li><a href="#服务器性能">服务器性能</a></li>
<li><a href="#jvm相关">JVM相关</a></li>
</ul>
</nav>
  </div>
</div>

    
    <div class="post-content">
      <p>最近参与了一个项目的压测工作，在此记录一下过程。</p>

<h1 id="数据库层面">数据库层面</h1>

<p>压测场景中涉及到不少数据查询逻辑，刚开始压测时由于SQL语句未做优化，导致一些后台请求直接挂起。通过druid监控和MySQL的<code>show full processlist</code>定位慢查询，然后进行SQL优化。另外，针对已有的慢查询，可以手动kill。</p>

<pre><code class="language-sql">SELECT CONCAT('kill ',id,';') FROM INFORMATION_SCHEMA.PROCESSLIST where info like 'select%'
</code></pre>

<p>对于SQL优化，通常做法就是加索引、in改join、or改union all等。笔者的做法是先解释一波SQL语句，定位慢在什么地方，譬如有全表扫描或扫描的数据行过多等，然后再进行索引的添加或SQL语句的调整。</p>

<h1 id="服务器性能">服务器性能</h1>

<p>最常用命令<code>top</code>，查看服务器的CPU负载、内存使用情况等。CPU负载高于CPU核心数或内存使用接近服务器内存时说明服务器性能达到了瓶颈。对于这种情况要么换性能更好的服务器，要么使用集群部署，将压力分摊到多台服务器上。但还有另一种情况，服务器性能可能并没有瓶颈，压测却压不上去，这种情况的原因可能是限制了JVM的最大堆内存，例如我们默认的配置为2G，这就导致对于单个JVM实例使用的最大内存只有2G。解决方法是可以适当的调大此配置，例如我们调大到8G。</p>

<h1 id="jvm相关">JVM相关</h1>

<p>JDK中提供了许多工具可以监控JVM的状态，例如<code>jstack</code>、<code>jstat</code>、<code>jmap</code>等。</p>

<p>查看JVM堆栈</p>

<pre><code class="language-sh">jstack pid &gt; login.txt
</code></pre>

<p>查看GC信息</p>

<pre><code class="language-sh">jstat -gcutil pid 3000 # 每3秒统计一下GC信息
</code></pre>
    </div>

    
    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">macd</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">2018-09-15</span>
  </p>
  
  
</div>

    
    
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    
      <label class="qr-code-image" for="reward">
        <img class="image" src="/img/reward/wechat-qr-code.png">
        <span>微信打赏</span>
      </label>
    
      <label class="qr-code-image" for="reward">
        <img class="image" src="/img/reward/alipay-qr-code.png">
        <span>支付宝打赏</span>
      </label>
  </div>
</div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
          <a href="/tags/%E6%80%A7%E8%83%BD%E6%8E%92%E6%9F%A5/">性能排查</a>
          
        </div>

      
      <nav class="post-nav">
        
        
          <a class="next" href="/wiki/java/idea%E5%92%8Ceclipse%E5%BF%AB%E6%8D%B7%E9%94%AE%E8%AE%B0%E5%BD%95/">
            <span class="next-text nav-default">IDEA和Eclipse快捷键</span>
            <span class="prev-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:macdfree@163.com" class="iconfont icon-email" title="email"></a>
      <a href="https://github.com/MACDfree" class="iconfont icon-github" title="github"></a>
  <a href="/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
    <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy; 
    
      2014 - 
    2019
    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">macd</span>
    <span class="division">|</span>
    <span style="margin-left: 5px;">
      <img alt="buildstatus" src="https://travis-ci.org/MACDfree/hugoblog.svg?branch=master">
    </span>
  </span>
</div>
    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
<script src="/lib/highlight/highlight.pack.js?v=20171001"></script><script type="text/javascript" src="/lib/jquery/jquery-3.2.1.min.js"></script>
  <script type="text/javascript" src="/lib/slideout/slideout-1.0.1.min.js"></script>
  <script type="text/javascript" src="/lib/fancybox/jquery.fancybox-3.1.20.min.js"></script>
<script type="text/javascript" src="/dist/even.min.js?v=3.1.1"></script>








</body>
</html>
